/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://app/jsmodules/sbProperties.jsm");

// check that gMM exists
if (typeof gMM == "undefined") {
  var gMM = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"].getService(Ci.sbIMediacoreManager);
}

var SuperSidebar = new Object();

SuperSidebar.onLoad = function() {
  this.contentBox = document.getElementById("supersidebar-contentbox");
  this.providerService = Cc["@necimal.com/supersidebar/providerservice;1"].getService().wrappedJSObject;
  
  // register the built-in content providers
  var ssLastfmBio = Cc["@necimal.com/supersidebar/lastfmbio;1"].getService().wrappedJSObject;
  this.providerService.registerProvider(ssLastfmBio);
  
  // listen for mediacore events
  gMM.addListener(this);
  
  // do we have a currently playing song?
  if ((gMM.status.state == Ci.sbIMediacoreStatus.STATUS_PLAYING) ||
      (gMM.status.state == Ci.sbIMediacoreStatus.STATUS_PAUSED) ||
      (gMM.status.state == Ci.sbIMediacoreStatus.STATUS_BUFFERING)) {
    this.onTrackChange(gMM.sequencer.currentItem);
  }
}

SuperSidebar.onUnload = function() {
  gMM.removeListener(this);
}

SuperSidebar.onMediacoreEvent = function(ev) {
  // handle mediacore events
  switch (ev.type) {
    case Ci.sbIMediacoreEvent.TRACK_CHANGE:
      this.onTrackChange(ev.data);
      break;
    case Ci.sbIMediacoreEvent.STREAM_END:
    case Ci.sbIMediacoreEvent.STREAM_STOP:
      this.onStop();
      break;
  }
}

SuperSidebar.onTrackChange = function(mediaItem) {
  // display artist name
  var artistName = mediaItem.getProperty(SBProperties.artistName);
  document.getElementById("artist-name").textContent = artistName;
  // get info from our content providers
  this.providerService.triggerProviders(mediaItem, this);
}

SuperSidebar.onStop = function() {
  // hide artist name
  document.getElementById("artist-name").textContent = "";
}

SuperSidebar.updateContent = function(ssProvider, content) {
  // look for an existing box for this provider
  var providerBox = document.getElementById(ssProvider.providerID);
  if (!providerBox) {
    providerBox = document.createElement("vbox");
    providerBox.setAttribute("id", ssProvider.providerID);
    providerBoxTitle = document.createElement("description");
    providerBoxTitle.setAttribute("class", "providertitle");
    providerBoxTitle.setAttribute("value", ssProvider.providerTitle);
    providerBox.appendChild(providerBoxTitle);
    this.contentBox.appendChild(providerBox);
  }
  
  // add the new content
  providerBox.appendChild(content);
}

// event wireup
window.addEventListener("load", function(e) { SuperSidebar.onLoad(); }, false);
window.addEventListener("unload", function(e) { SuperSidebar.onUnload(); }, false);
